package gao.xiaolei.quertz;

import gao.xiaolei.dto.Task;
import gao.xiaolei.dto.TaskInDto;
import gao.xiaolei.util.ServiceName;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import java.time.LocalDateTime;
import java.util.List;

//用于定时执行向某人发送任务
@Slf4j
@DisallowConcurrentExecution
public class SendTaskJob implements Job {
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("------------------------------进入到发送任务的job类,时间：".concat(LocalDateTime.now().toString()));
        JobDataMap data=jobExecutionContext.getJobDetail().getJobDataMap();
        TaskInDto task=(TaskInDto)data.get("task");
        HttpHeaders headers=new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<TaskInDto> httpEntity=new HttpEntity<>(task,headers);
        restTemplate.postForObject(ServiceName.TASK_SERVICE.getServiceName(),httpEntity,Object.class);
        log.info("------------------------------退出发送任务的job类,时间：".concat(LocalDateTime.now().toString()));
    }
}
